﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ELS.User;

namespace ELS.Template
{
    public partial class ListTemplateForm : Form
    {
        Panel parentPanel;
        TextBox textBox;
        PictureBox pictureBox;

        TemplateController templateController;
        // Page
        private int totalRecords = 0;
        private int pageSize = 15;
        private int pageCount = 0;
        private int currentPage = 1;
        
        public ListTemplateForm(TextBox textBox, PictureBox pictureBox, Panel pnlTemplateForm)
        {
            templateController = new TemplateController();
            this.textBox = textBox;
            this.pictureBox = pictureBox;
            parentPanel = pnlTemplateForm;

            InitializeComponent();

            SetupPagingInformation();
            LoadDataGridViewPaging();
        }

        private void btnCreateNewTemplate_Click(object sender, EventArgs e)
        {
            foreach (Control ctl in parentPanel.Controls)
            {
                ctl.Dispose();
            }

            AddEditTemplateForm createNewForm = new AddEditTemplateForm(textBox, pictureBox, parentPanel);
            createNewForm.TopLevel = false;
            createNewForm.Parent = parentPanel;
            createNewForm.Visible = true;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            bool isSelectedTemplate = GetListRowSelect().Count > 0;
            if (isSelectedTemplate)
            {
                bool isCanEdit = GetListRowSelect().Count == 1;
                if (isCanEdit)
                {
                    string selectedTemplate = GetListRowSelect()[0];
                    foreach (Control ctl in parentPanel.Controls)
                    {
                        ctl.Dispose();
                    }

                    AddEditQuestionForm editForm = new AddEditQuestionForm(textBox, pictureBox, parentPanel);
                    editForm.TopLevel = false;
                    editForm.Parent = parentPanel;
                    editForm.Visible = true;
                }
                else
                {
                    UserCommon.NotifyOnMainForm(textBox, pictureBox, UserCommon.SelectUpdateTemplate_NotMulti, 0);
                }
            }
            else
            {
                UserCommon.NotifyOnMainForm(textBox, pictureBox, UserCommon.SelectUpdateTemplate_DoNotSelect, 0);
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            bool isSelectedTemplate = GetListRowSelect().Count > 0;
            if (isSelectedTemplate)
            {
                DialogResult dialogConfirm = MessageBox.Show(UserCommon.DeleteTemplate_Message, UserCommon.DeleteTemplate_MessageBox_Title, MessageBoxButtons.YesNo);
                if (dialogConfirm == DialogResult.Yes)
                {
                    bool isDeleteSuccess = templateController.DeleteSelectedTemplate(GetListRowSelect());
                    if (isDeleteSuccess)
                    {
                        UserCommon.NotifyOnMainForm(textBox, pictureBox, UserCommon.DeleteTemplate_Success, 1);

                        // Reload content
                        SetupPagingInformation();
                        LoadDataGridViewPaging();
                    }
                    else
                    {
                        UserCommon.NotifyOnMainForm(textBox, pictureBox, UserCommon.DeleteTemplate_Fail, -1);
                    }
                }
                else if (dialogConfirm == DialogResult.No)
                {
                    return;
                }
            }
            else
            {
                UserCommon.NotifyOnMainForm(textBox, pictureBox, UserCommon.DeleteUpdateTemplate_DoNotSelect, 0);
            }
        }

        /// <summary>
        /// Gets the list row select.
        /// </summary>
        /// <returns>List{System.String}.</returns>
        private List<string> GetListRowSelect()
        {
            List<string> rowsSelect = new List<string>();
            rowsSelect.Clear();
            foreach (DataGridViewRow row in drgListTemplate.Rows)
            {
                if (Convert.ToBoolean(row.Cells[0].Value) == true)
                {
                    rowsSelect.Add(row.Cells[1].Value.ToString());
                }
            }
            return rowsSelect;
        }

        // PAGING Implementation

        private void btnFirst_Click(object sender, EventArgs e)
        {
            this.goFirst();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            this.goPrevious();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            this.goNext();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            this.goLast();
        }

        private int CountAllTemplates()
        {
            return templateController.CountAllTemplates();
        }

        private int CountAllTemplatesByKeyword(string keyword)
        {
            return templateController.CountAllTemplatesByKeyword(keyword);
        }

        private void SetupPagingInformation()
        {
            // For Page view.
            if (promptedTxtSearchTemplate.Text != null && promptedTxtSearchTemplate.Text.Length > 0)
            {//search by keyword
                this.totalRecords = CountAllTemplatesByKeyword(promptedTxtSearchTemplate.Text);
            }
            else
            {// default load
                this.totalRecords = CountAllTemplates();
            }

            this.pageCount = this.totalRecords / this.pageSize;
            // Adjust page count if the last page contains partial page.
            if (this.totalRecords % this.pageSize > 0)
                this.pageCount++;
            this.currentPage = 0;
        }

        /// <summary>
        /// Loads the data grid view.
        /// </summary>
        private void LoadDataGridViewPaging()
        {
            int intSkip = 0;
            intSkip = (this.currentPage * this.pageSize);

            drgListTemplate.AutoGenerateColumns = false;
            if (promptedTxtSearchTemplate.Text != null && promptedTxtSearchTemplate.Text.Length > 0)
            { //search by keyword
                drgListTemplate.DataSource = templateController.GetAllAvailableTemplatesByKeywordPaging(promptedTxtSearchTemplate.Text, this.pageSize, intSkip);
            }
            else
            { // default load
                drgListTemplate.DataSource = templateController.GetAllTemplatesPaging(this.pageSize, intSkip);
            }

            // Show Status
            this.lblStatus.Text = (this.currentPage + 1).ToString() + " / " + this.pageCount.ToString();
        }

        private void goFirst()
        {
            this.currentPage = 0;

            LoadDataGridViewPaging();
        }

        private void goPrevious()
        {
            if (this.currentPage == this.pageCount)
                this.currentPage = this.pageCount - 1;

            this.currentPage--;

            if (this.currentPage < 1)
                this.currentPage = 0;

            LoadDataGridViewPaging();
        }

        private void goNext()
        {
            this.currentPage++;

            if (this.currentPage > (this.pageCount - 1))
                this.currentPage = this.pageCount - 1;

            LoadDataGridViewPaging();
        }

        private void goLast()
        {
            this.currentPage = this.pageCount - 1;

            LoadDataGridViewPaging();
        }

        private void promptedTxtSearchTemplate_TextChanged(object sender, EventArgs e)
        {
            string keyword = "";
            keyword = promptedTxtSearchTemplate.Text;

            SetupPagingInformation();
            LoadDataGridViewPaging();
        }

        private void promptedTxtSearchTemplate_Leave(object sender, EventArgs e)
        {
            bool isHasText = promptedTxtSearchTemplate.Text.Length > 0;
            if (!isHasText)
            {
                promptedTxtSearchTemplate.PromptText = UserCommon.SearchTemplate_PromoteText;
            }
        }

        private void drgListTemplate_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 2 && e.RowIndex >= 0)
            {
                string selectedTemplateID = (drgListTemplate.Rows[e.RowIndex].Cells[1].Value.ToString());

                foreach (Control ctl in parentPanel.Controls)
                {
                    ctl.Dispose();
                }

                AddEditTemplateForm editForm = new AddEditTemplateForm(selectedTemplateID, textBox, pictureBox, parentPanel);
                editForm.TopLevel = false;
                editForm.Parent = parentPanel;
                editForm.Visible = true;
            }
        }

        private void drgListTemplate_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            drgListTemplate.ClearSelection();
        }
    }
}
